package com.example.demo.thread.lock.lockInterruptibly;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author luowj12
 * @since 2021/5/13 15:40
 */
public class Lock01 implements Runnable{

    private static Lock lock = new ReentrantLock();

    @Override
    public void run(){
        try{
            System.out.println(Thread.currentThread().getName() + "trying to get lock");
            lock.lock();

            Thread.sleep(5000);
            System.out.println(Thread.currentThread().getName() + " finished");
        } catch (InterruptedException e){
            System.out.println(Thread.currentThread().getName() + " interrupted");
        }
        finally{
            lock.unlock();
            System.out.println("now lock is avalibale");
        }
    }


}
